<?php
/**
 * FTP类 实现后台ftp上传
 */

class Ftp{
	private $ftp_server="42.96.149.21"; 
	private $ftp_user_name="alork"; 
	private $ftp_user_pass="alorkwang";
	private $conn_id;
	private $webname = '';     //站点名称

	public function __construct($webname)
	{
		$this->webname = $webname;

		// set up basic connection 
		$this->conn_id = ftp_connect($this->ftp_server); 

		// login with username and password 
		$login_result = ftp_login($this->conn_id, $this->ftp_user_name, $this->ftp_user_pass); 

		if (!$this->conn_id || !$login_result) {
			throw new Exception("Couldn't connect to $this->ftp_server");
		}
	}

	/**
	 * 上传到远程
	 * @param  [type] $local_file  本地文件
	 * @param  [type] $remote_file 远端文件
	 * @return [type]              成功返回true,否则返回false
	 */
	public function remote_upload($local_file, $remote_file)
	{
		// upload a file 
		if (!ftp_put($this->conn_id, $this->webname . '/public_html/' . $remote_file, $local_file, FTP_BINARY)) { 
		    return false;
		} 
		// close the connection 
		ftp_close($this->conn_id); 

		return true;
	}

    /**
     * 重命名/移动
     *
     * @access  public
     * @param   string  远程目录标识(ftp)
     * @param   string  新目录标识
     * @param   boolean 判断是重命名(false)还是移动(true)
     * @return  boolean
     */
    public function rename($oldname, $newname, $move = false) {
        if (!$this->conn_id) {
            return false;
        }

        $result = @ftp_rename($this->conn_id, $oldname, $newname);

        if ($result === false) {
            return false;
        }
        return true;
    }
}
